<?php
/**
 * 功能：后台商品规格控制器
 * 作者：魏安来
 * 日期：2017/01/09
 */

namespace app\index\controller;
use think\Db;

class GoodsOption extends Base {

	/*商品规格列表*/
	public function index() {
		//调用左侧菜单
		$menuController = new Menu();
		$menuController->sidebar();
		$modelInfo = Db::name('goods_model')->select();
		$this->assign('modelInfo', $modelInfo);
		/*显示商品属性列表*/
		//搜索条件
		$where = '1=1';
		$model_id = request()->param('model_id');
		if (!empty($model_id)) {
			$where .= ' and model_id='.$model_id;
			$searchstr = '&model_id='.$model_id;
		} else {
			$searchstr = '';
		}
		$this->assign('model_id', $model_id);
		//商品规格总数
		$count = count(Db::name('goods_option')->where($where)->select());
		//每页显示的条数
		$pageCount = 3;
		//当前页码
		$currentPage = request()->param('page') ? request()->param('page') : 1;
		//数据表
		$table = 'goods_option';
		//分页数据
		$optionInfo = page($count, $pageCount, $currentPage, $table, false, $where);
		//追加模型名称
		if (!empty($optionInfo)) {
			foreach ($optionInfo as $k=>$v) {
				$modelArr = Db::name('goods_model')->where('id', $v['model_id'])->find();
				$optionInfo[$k]['model_name'] = $modelArr['name'];
			}
		}
		//分页html代码
		$pageHtml = page($count, $pageCount, $currentPage, $table, true, $where, $searchstr);
		$this->assign('optionInfo', $optionInfo);
		$this->assign('pageHtml', $pageHtml);
		return $this->fetch();
	}

	/*商品规格添加*/
	public function add() {
		$modelInfo = Db::name('goods_model')->select();
		$this->assign('modelInfo', $modelInfo);
		if (request()->isAjax()) {
			//接收数据
			$data = input('post.');
			//定义验证规则
            $rule = [
            	'model_id' => 'notIn:0',
				'name' => 'require',
				'option' => 'require'
            ];

            //定义错误提示信息
            $message = [
            	'model_id' => '请选择所属模型',
                'name.require' => '规格名称不能为空',
				'option.require' => '规格项必须填写'
            ];
            $validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
            if ($result) {
            	//写入数据库
				$affectRow = Db::name('goods_option')->insert($data);
				if ($affectRow) {
					return 'succ';
				} else {
					return 'error';
				}
            } else {
            	return $this->getError();
            }
		}
		return $this->fetch();
	}

	/*商品规格编辑*/
	public function edit() {
		$modelInfo = Db::name('goods_model')->select();
		$this->assign('modelInfo', $modelInfo);
		$id = request()->param('id');
		$optionInfo = Db::name('goods_option')->where('id', $id)->find();
		$this->assign('model_id',$optionInfo['model_id']);
		$this->assign('name', $optionInfo['name']);
		$this->assign('option', $optionInfo['option']);
		$this->assign('id', $id);
		if (request()->isAjax()) {
			//接收数据
			$data = input('post.');
			//定义验证规则
            $rule = [
            	'model_id' => 'notIn:0',
				'name' => 'require|min:2',
				'option' => 'require'
            ];

            //定义错误提示信息
            $message = [
            	'model_id' => '请选择所属模型',
                'name.require' => '规格名称不能为空',
                'option.require' => '规格项不能为空'
            ];
            $validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
			unset($data['id']);
            if ($result) {
            	//写入数据库
				$affectRow = Db::name('goods_option')->where('id',input('id'))->update($data);
				if ($affectRow) {
					return 'succ';
				} else {
					return 'error';
				}
            } else {
            	return $this->getError();
            }
		}
		return $this->fetch();
	}

	/*商品规格删除*/
	public function delete() {
		if (request()->isAjax()) {
			$result = Db::name('goods_option')->delete(input('id'));
			if ($result) {
				return 'succ';
			} else {
				return 'error';
			}
		}
	}

}

?>